package net.sf.rose.business.bean.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.simpleframework.xml.Attribute;

import net.sf.rose.jdbc.DataParser;
import net.sf.rose.jdbc.service.Service;

/**
 * @author fengjian
 * @since 2014-3-27
 */
public abstract class Action {

	/** 名称 */
	@Attribute
	private String name = null;

	/** 操作分类(包括set,search,update,define) */
	@Attribute
	private String actionType = null;

	/** 分类 */
	@Attribute(required = false)
	private String sort = null;

	/** 说明 */
	@Attribute(required = false)
	private String description = null;

	/**
	 * 执行自定义操作
	 * @param request   - Http请求
	 * @param response  - Http返回
	 * @param service   - 数据库连接服务类 
	 * @param map       - 前台传过来的数据
	 * @param data      - 上下文返回的数据
	 */
	public abstract void execute(HttpServletRequest request, HttpServletResponse response, 
			Service service, Map<String, Object> map, Map<String, Object> data);

	/**
	 * 将数据定义集中的数据转化成对应的数据值集
	 * @param fields 	- 数据定义类集合
	 * @return map		- 查询结果数据类
	 */
	public Map<String, Object> parseField(List<FieldBean> fields) {
		Map<String, Object> m = new HashMap<String, Object>();
		if (fields != null) {
			DataParser paser = new DataParser();
			for (FieldBean field : fields) {
				if (field.getType() != null) {
					m.put(field.getKey(), paser.parse(field.getType(), field.getValue(), field.getFormat()));
				} else {
					m.put(field.getKey(), field.getValue());
				}
			}
		}
		return m;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
